import http from '../../apis/api.js';
import util from '../../utils/util.js';
let self;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    /**分页数据 */
    pagination: {
      currentPage: 1,
      pageSize: 6,
      more: 0
    },
    state: null,
    loading: false,
    repairList: []
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    self = this;
    self.data.state = Number(options.state),
      self.setData({
        state: self.data.state
      })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    self.data.pagination.currentPage = 1;
    self.data.repairList = [];
    self.getRepairList();
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    if (self.data.pagination.more > 0) {
      self.data.pagination.currentPage++;
      self.getRepairList();
    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  /**获取保养单列表*/
  getRepairList() {
    let postData = {
      currentPage: self.data.pagination.currentPage,
      pageSize: self.data.pagination.pageSize,
      state: self.data.state
    }
    self.setData({
      'loading': true
    })
    http.maintainList(postData).then(res => {
      self.data.repairList = self.data.repairList.concat(res.content.records);
      self.data.pagination.more = Number(res.content.current) * Number(res.content.size) < res.content.total ? 1 : 0;
      self.setData({
        'loading': false,
        'repairList': self.data.repairList,
        'pagination': self.data.pagination
      })
    })
  },
  optionHandle(e) {
    let opt = e.currentTarget.dataset;
    switch (opt.type) {
      case 'add':
        wx.navigateTo({
          url: `/pages/addMaintain/addMaintain`,
        })
        break;
      case 'detail':
        wx.navigateTo({
          url: `/pages/maintainDetail/maintainDetail?id=${opt.id}`,
        })
        break;
      case 'delete':
        wx.showModal({
          title: '提示',
          content: '是否删除保修单？',
          confirmColor: '#f05855',
          success(res) {
            if (res.confirm) {
              http.deleteRepair(opt.id).then(res => {
                wx.showToast({
                  title: '删除成功',
                  icon: 'success',
                  duration: 2000
                })
                self.data.pagination.currentPage = 1;
                self.data.repairList = [];
                self.getRepairList();
              })
            } else if (res.cancel) {
            }
          }
        })
        break;
    }
  },
  /**切换tab*/
  changeState(e) {
    self.data.state = e.currentTarget.dataset.state ? Number(e.currentTarget.dataset.state) : null;
    self.data.pagination.currentPage = 1;
    self.data.repairList = [];
    console.log(self.data.state);
    self.setData({
      state: self.data.state
    })
    self.getRepairList();
  }
})